home *** CD-ROM | disk | FTP | other *** search
/ TPUG - Toronto PET Users Group / TPUG Users Group CD / TPUG Users Group CD.iso / AMIGA / AMICUS / AMICUS05.ADF / IFF / gio.c < prev    next >
C/C++ Source or Header  |  1986-04-20  |  5KB  |  130 lines

  1.  
  2. /*----------------------------------------------------------------------*/
  3. /* GIO.C  Generic I/O Speed Up Package                         1/23/86  */
  4. /* See GIOCall.C for an example of usage.                   */
  5. /* Read not speeded-up yet.  Only one Write file buffered at a time.  */
  6. /* Note: The speed-up provided is ONLY significant for code such as IFF */
  7. /* which does numerous small Writes and Seeks.                   */
  8. /*                                                               */
  9. /* By Jerry Morrison and Steve Shaw, Electronic Arts.            */
  10. /* This software is in the public domain.                        */
  11. /*                                                               */
  12. /* This version for the Commodore-Amiga computer.                */
  13. /*                                                                     */
  14. /*----------------------------------------------------------------------*/
  15. #include "iff/gio.h"     /* See comments here for explanation.*/
  16.  
  17. #if GIO_ACTIVE
  18.  
  19. #define local static
  20.  
  21.  
  22. local BPTR wFile      = NULL;
  23. local BYTE *wBuffer   = NULL;
  24. local LONG wNBytes    = 0; /* buffer size in bytes.*/
  25. local LONG wIndex     = 0; /* index of next available byte.*/
  26. local LONG wWaterline = 0; /* Count of # bytes to be written.
  27.                    * Different than wIndex because of GSeek.*/
  28.  
  29. /*----------- GOpen ----------------------------------------------------*/
  30. LONG GOpen(filename, openmode)   char *filename;  LONG openmode; {
  31.     return( Open(filename, openmode) );
  32.     }
  33.  
  34. /*----------- GClose ---------------------------------------------------*/
  35. LONG GClose(file)  BPTR file; {
  36.     LONG signal = 0, signal2;
  37.     if (file == wFile)
  38.      signal = GWriteUndeclare(file);
  39.     signal2 = Close(file);    /* Call Close even if trouble with write.*/
  40.     if (signal2 < 0)
  41.      signal = signal2;
  42.     return( signal );
  43.     }
  44.  
  45. /*----------- GRead ----------------------------------------------------*/
  46. LONG GRead(file, buffer, nBytes)   BPTR file;  BYTE *buffer;  LONG nBytes; {
  47.     LONG signal = 0;
  48.     /* We don't yet read directly from the buffer, so flush it to disk and
  49.      * let the DOS fetch it back. */
  50.     if (file == wFile)
  51.      signal = GWriteFlush(file);
  52.     if (signal >= 0)
  53.      signal = Read(file, buffer, nBytes);
  54.     return( signal );
  55.     }
  56.  
  57. /* ---------- GWriteFlush ----------------------------------------------*/
  58. LONG GWriteFlush(file)  BPTR file; {
  59.     LONG gWrite = 0;
  60.     if (wFile != NULL  &&  wBuffer != NULL  &&  wIndex > 0)
  61.      gWrite = Write(wFile, wBuffer, wWaterline);
  62.     wWaterline = wIndex = 0;  /* No matter what, make sure this happens.*/
  63.     return( gWrite );
  64.     }
  65.  
  66. /* ---------- GWriteDeclare --------------------------------------------*/
  67. LONG GWriteDeclare(file, buffer, nBytes)
  68.     BPTR file;  BYTE *buffer;  LONG nBytes; {
  69.     LONG gWrite = GWriteFlush(wFile);  /* Finish any existing usage.*/
  70.     if ( file==NULL  ||  (file==wFile  &&  buffer==NULL)  ||  nBytes<=3) {
  71.      wFile = NULL;   wBuffer = NULL;     wNBytes = 0; }
  72.     else {
  73.      wFile = file;   wBuffer = buffer;   wNBytes = nBytes; }
  74.     return( gWrite );
  75.     }
  76.  
  77. /* ---------- GWrite ---------------------------------------------------*/
  78. LONG GWrite(file, buffer, nBytes)   BPTR file;  BYTE *buffer;  LONG nBytes; {
  79.     LONG gWrite = 0;
  80.  
  81.     if (file == wFile  &&  wBuffer != NULL) {
  82.      if (wNBytes >= wIndex + nBytes) {
  83.          /* Append to wBuffer.*/
  84.          movmem(buffer, wBuffer+wIndex, nBytes);
  85.          wIndex += nBytes;
  86.          if (wIndex > wWaterline)
  87.           wWaterline = wIndex;
  88.          nBytes = 0;          /* Indicate data has been swallowed.*/
  89.          }
  90.      else {
  91.          wWaterline = wIndex;     /* We are about to overwrite any
  92.           * data above wIndex, up to at least the buffer end.*/
  93.          gWrite = GWriteFlush(file);  /* Write data out in proper order.*/
  94.          }
  95.      }
  96.     if (nBytes > 0  &&  gWrite >= 0)
  97.      gWrite += Write(file, buffer, nBytes);
  98.     return( gWrite );
  99.     }
  100.  
  101. /* ---------- GSeek ----------------------------------------------------*/
  102. LONG GSeek(file, position, mode)
  103.     BPTR file;   LONG position;   LONG mode; {
  104.     LONG gSeek = -2;
  105.     LONG newWIndex = wIndex + position;
  106.  
  107.     if (file == wFile  &&  wBuffer != NULL) {
  108.      if (mode == OFFSET_CURRENT  &&
  109.          newWIndex >= 0  &&  newWIndex <= wWaterline) {
  110.          gSeek = wIndex;      /* Okay; return *OLD* position */
  111.          wIndex = newWIndex;
  112.          }
  113.      else {
  114.          /* We don't even try to optimize the other cases.*/
  115.          gSeek = GWriteFlush(file);
  116.          if (gSeek >= 0)   gSeek = -2;  /* OK so far */
  117.          }
  118.      }
  119.     if (gSeek == -2)
  120.      gSeek = Seek(file, position, mode);
  121.     return( gSeek );
  122.     }
  123.  
  124. #else /* not GIO_ACTIVE */
  125.  
  126. void GIODummy() { } /* to keep the compiler happy */
  127.  
  128. #endif GIO_ACTIVE
  129.  
  130.